(function(){
	$(function(){
		var mobileInput = $('#mobile-number')
		var codeInput = $('#test-code')
		var passInput = $('#set-password')
		var repassInput = $('#reset-password')
		var getCodeButton = $('.code-button')
		var errorArea = $('.alarm-word b')
		var defaultMessage = '请填写您的信息'
		var defaultGetCodeMeassage = getCodeButton.text()
		var codeTime = 30;

		errorArea.text(defaultMessage)

		//手机号
		function checkTel(callback){
		    var tel=mobileInput.val();
		    api.solo('checkTel',[tel,'reset'],function(d){
		        if(d.state==200){
		            if(callback) callback();
		        }else{
		            errorArea.text(d.title)
		        }
		    })
		}
		mobileInput.change(function(event) {
		    checkTel()
		});

		//发送验证码
		getCodeButton.click(function(e){
		    e.preventDefault()
		    var tel=mobileInput.val();
		    var type='reset';
		    checkTel(function(){
		        api.solo('getCode',[tel,type],function(d){
		            if(d.state==200){
		                alert('验证码已发送请查收')
		            }else{
		                errorArea.text(d.title)
		            }
		        })
		    })
		    
		})

		//验证验证码
		codeInput.bind("input propertyChange", function(){
		    var tel=mobileInput.val();
		    var code = codeInput.val();
		    if(code.length!=6){
		        $('.code-right').hide();
		        $('.code-wrong').show();
		        errorArea.text("验证码错误");
		        return false;
		    }
		    api.solo('checkCode',[tel,code],function(d){
		        if(d.state==200){
		            $('.code-right').show();
		            $('.code-wrong').hide();
		            errorArea.text("");
		        }else{
		            $('.code-right').hide();
		            $('.code-wrong').show();
		            errorArea.text(d.title)
		        }
		    })
		})

		//提交验证
		//console.log($('#register-box form'))
		var regForm  = $('#reset-form')
		regForm.submit(function(event) {
		    var tel  = mobileInput.val();
		    var code = codeInput.val();
		    var pass = passInput.val()
		    var repass = repassInput.val()
		    if(!tel||!code||!pass||!repass){
		        alert('请填写完整')
		        event.preventDefault()
		    }

		    if(pass!=repass){
		        errorArea.text('两次输的密码不一样')
		        event.preventDefault()
		    }else if(pass.length<6){
		        errorArea.text('密码不少于六位')
		        event.preventDefault()
		    }
		    return true;
		});
	})
})()